<style>
.messageTemplate .el-input.is-disabled .el-input__inner{
  color:black;
}
.messageTemplate .el-textarea.is-disabled .el-textarea__inner{
  color:black;
}
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="消息类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择消息类型" clearable size="small">
          <el-option
          v-for="dict in msgTypeOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模板名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入模板编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['message:template:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['message:template:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleUpdateStatus(true)"
          v-hasPermi="['message:template:remove']"
        >弃用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleUpdateStatus(false)"
          v-hasPermi="['message:template:remove']"
        >恢复</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['message:template:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange" :border="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="消息类型" align="center" prop="type" :formatter="msgTypeFmt" />
      <el-table-column label="模板名称" align="center" prop="name" />
      <el-table-column label="模板编号" align="center" prop="no" />
      <el-table-column label="关键字" align="center" prop="keyword" />
      <el-table-column label="状态" align="center" prop="status" :formatter="msgStatusFmt" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['message:template:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.examineResult == 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdateAudit(scope.row)"
            v-hasPermi="['merchant:template:audit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['message:examine:query']"
          >详细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['message:examine:remove']"
          >{{scope.row.status == 1?'弃用':'恢复'}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改消息模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="消息类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择消息类型">
            <el-option
            v-for="dict in msgTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="消息模板名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入消息模板名称" />
        </el-form-item>
        <el-form-item v-if="!form.id" label="消息模板编号" prop="no">
          <el-input v-model.trim="form.no" placeholder="请输入消息模板编号" />
        </el-form-item>
        <el-form-item label="消息关键字" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入消息关键字" />
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入消息内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button v-if="form.examineResult == 0" type="primary" @click="handleUpdateViewAudit">审 核</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="500px" append-to-body>
      <el-form class="messageTemplate" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="消息类型" prop="type">
          <el-select disabled v-model="form.type" placeholder="请选择消息类型">
            <el-option
            v-for="dict in msgTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="消息模板名称" prop="name">
          <el-input disabled v-model="form.name" placeholder="请输入消息模板名称" />
        </el-form-item>
        <el-form-item label="消息模板编号" prop="no">
          <el-input disabled v-model="form.no" placeholder="请输入消息模板编号" />
        </el-form-item>
        <el-form-item label="消息关键字" prop="keyword">
          <el-input disabled v-model="form.keyword" placeholder="请输入消息关键字" />
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <el-input disabled v-model="form.content" type="textarea" placeholder="请输入消息内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确 定</el-button>
        <el-button v-if="form.examineResult == 0" type="primary" @click="handleUpdateViewAudit">审 核</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog :title="title" :visible.sync="auditOpen" width="500px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="80px">
        <el-form-item label="审核结果" prop="examineResult">
          <el-select v-model="auditForm.examineResult" >
            <el-option
            v-for="dict in exaResuOptionsAddAndUpdate"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见" prop="examineConclusion">
          <el-input v-model="auditForm.examineConclusion" />
        </el-form-item>
        <el-form-item label="审核截止日期" prop="examineEndDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="auditForm.examineEndDate"
            type="date"
            value-format="yyyyMMdd"
            >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="auditFormSubmit" v-hasPermi="['merchant:template:audit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTemplate, getTemplate, delTemplate, addTemplate, updateTemplate, exportTemplate, logicRemoveTemplateByNos, logicRecoverTemplateByNos, auditTemplate, listTemplateVo, getTemplateVo } from "@/api/message/template";

export default {
  name: "Template",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组
      nos: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息模板表格数据
      templateList: [],
      // 消息类型列表
      msgTypeOptions: [],
      // 消息模板状态
      msgStatusOptions: [],
      // 审核结果字典
      exaResuOptions: [],
      // 审核结果字典
      exaResuOptionsAddAndUpdate: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      auditOpen: false,
      // 是否显示弹出层
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: null,
        no: null,
        keyword: null,
        content: null,
        status: null,
        examineCode: null,
        buildOperator: null,
        buildDate: null,
        buildTime: null,
        modifiedOperator: null,
        modifiedDate: null,
        modifiedTime: null
      },
      // 表单参数
      form: {},
      // 审核表单参数
      auditForm: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "消息类型不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "消息模板名称不能为空", trigger: "blur" }
        ],
        no: [
          { required: true, message: "消息模板编号不能为空", trigger: "blur" }
        ],
        keyword: [
          { required: true, message: "消息关键字不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "消息内容不能为空", trigger: "blur" }
        ]
      },
      //商户审核信息表单校验
      auditRules: {
        examineResult: [
          { required: true, message: "审核结果不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("msg_message_type").then(response => {
      this.msgTypeOptions = response.data;
    });
    this.getDicts("msg_message_template_status").then(response => {
      this.msgStatusOptions = response.data;
    });
    this.getDicts("msg_message_template_examineResult_type").then(response => {
      this.exaResuOptions = response.data;
      this.exaResuOptions.forEach(element => {
        if(element.dictValue !=0){
          this.exaResuOptionsAddAndUpdate.push(element)
        }
      });
    });
  },
  methods: {
    /** 查询消息模板列表 */
    getList() {
      this.loading = true;
      listTemplateVo(this.queryParams).then(response => {
        this.templateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openView = false;
      this.auditOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        name: null,
        no: null,
        keyword: null,
        content: null,
        status: "0",
        examineCode: null,
        buildOperator: null,
        buildDate: null,
        buildTime: null,
        modifiedOperator: null,
        modifiedDate: null,
        modifiedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.nos = selection.map(item => item.no)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消息模板";
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getTemplateVo(id).then(response => {
        this.form = response.data;
        this.openView = true;
        this.title = "修改消息模板";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTemplateVo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息模板";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTemplate(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemplate(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审核按钮 */
    handleUpdateAudit(row) {
      const nos = row? row.no? [row.no] : this.nos : this.nos;
      this.auditForm.infoNos = nos;
      this.auditOpen = true;
    },
    /** 详情审核按钮 */
    handleUpdateViewAudit() {
      this.auditForm.infoNos = [this.form.no];
      this.auditOpen = true;
    },
    /** 审核提交按钮 */
    auditFormSubmit(row) {
      this.$refs.auditForm.validate(valid => {
        if(valid){
          auditTemplate(this.auditForm).then(response => {
            this.msgSuccess("审核成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const nos = row.no || this.nos;
      if(row.status == 1){
        this.$confirm('是否确认删除消息模板编号为"' + nos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicRemoveTemplateByNos(nos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      }else if(row.status == 2){
        this.$confirm('是否确认恢复消息模板编号为"' + nos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicRecoverTemplateByNos(nos);
        }).then(() => {
          this.getList();
          this.msgSuccess("恢复成功");
        }).catch(() => {});
      }
    },
    /** 删除按钮操作 */
    handleUpdateStatus(isRemove) {
      var nos = this.nos;
      var newNos = [];
      nos.forEach(no => {
        //标记是否添加到数组中
        var isAdd = false;
        this.templateList.forEach(element => {
          if(no == element.no){
            if(isRemove&&element.status == 1){
              isAdd = true;
            }else if(!isRemove&&element.status == 2){
              isAdd = true;
            }
          }
        });
        if(isAdd){
          newNos.push(no);
        }
      });
      nos = newNos;
      if(nos.length == 0){
        if(isRemove){
          this.msgSuccess("已删除");
        }else{
          this.msgSuccess("已恢复");
        }
        return;
      }
      if(isRemove){
        this.$confirm('是否确认删除消息模板编号为"' + nos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicRemoveTemplateByNos(nos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      }else{
        this.$confirm('是否确认恢复消息模板编号为"' + nos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicRecoverTemplateByNos(nos);
        }).then(() => {
          this.getList();
          this.msgSuccess("恢复成功");
        }).catch(() => {});
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有消息模板数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTemplate(queryParams);
        }).catch(() => {});
    },
    /** 门店状态格式化 */
    msgTypeFmt(row, column) {
      var type = row.type;
      if(!type){
        return "";
      }
      var val = type.trim();
      this.msgTypeOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 门店类型格式化 */
    msgStatusFmt(row, column) {
      var status = row.status;
      if(!status){
        return "";
      }
      var val = status.trim();
      this.msgStatusOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    }
  }
};
</script>
